package CreatorUtil;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import persistent.Db4oServletListener;
import persistent.Patient;
import LoginUtil.User;
import LoginUtil.User.UserType;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;

/**
 * Servlet implementation class EditUserServlet
 */
public class EditUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ObjectContainer container = 
	        (ObjectContainer)request.getAttribute(Db4oServletListener.KEY_DB4O_SESSION);
		
		final String username = request.getParameter("usernamebox");
		
		ObjectSet<Patient> rs = container.query(new Predicate<Patient>(){
				public boolean match(Patient user) {
					return user.getUsername().equals(username);}});
		
		// make sure user exists
		if(rs.size() > 0) {
			Patient patient = rs.next();
			patient.setFirstName(request.getParameter("firstnamebox"));
			patient.setLastName(request.getParameter("lastnamebox"));
			patient.setAddress(request.getParameter("addressbox"));
			patient.setAge(Integer.parseInt(request.getParameter("agebox")));
			patient.setAllergies(request.getParameter("allergiesbox"));
			patient.setEmail(request.getParameter("emailbox"));
			patient.setGender(request.getParameter("genderbox"));
			patient.setInsurance(request.getParameter("insurancebox"));
			patient.setNumber(request.getParameter("phonebox"));
			patient.setPharmacy(request.getParameter("pharmacybox"));

			container.store(patient);
			container.commit();

			request.getSession().removeAttribute("KEY_VIEWING_PATIENT");
			request.getSession().setAttribute("KEY_VIEWING_PATIENT", patient);
			response.sendRedirect("viewPatient.jsp");
		} else {

			// create an error page in response to invalid username
			PrintWriter rw = response.getWriter();
			rw.append("<html><head>" +
					"<meta http-equiv=\"Content-Type\"" + 
					"content=\"text/html; charset=windows-1256\">" +
					"<title>  Error - Adventure Time MedSys  </title>" +
					"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" +
			"</head><body><center><br><br>There was an error editing the user<br>Please press back on your browser</body></center>");
			rw.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

private UserType parseUserType(String parameter) {
	UserType type;
	if(parameter.equalsIgnoreCase("admin")) type = UserType.Admin;
	else if(parameter.equalsIgnoreCase("doctor")) type = UserType.Doctor;
	else if(parameter.equalsIgnoreCase("nurse")) type = UserType.Nurse;
	else if(parameter.equalsIgnoreCase("patient")) type = UserType.Patient;
	else type = UserType.Invalid;
	return type;
}

}